﻿using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Core;
using DocumentFormat.OpenXml.Lite.Core.Interfaces;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table
{
    [XmlRoot("tc", Namespace = Constants.WordMain)]
    public class TableCell : XmlSerializable, IOfficeElement
    {
        [XmlIgnore]
        public Guid DocumentId { get; set; }

        [Optional]
        [XmlElement("tcPr", Namespace = Constants.WordMain)]
        public TableCellProperties CellProperty { get; set; }

        [Optional]
        [XmlElement("tbl", Namespace = Constants.WordMain)]
        public XmlList<Table> Tables { get; set; }

        [Optional]
        [XmlElement("p", Namespace = Constants.WordMain)]
        public XmlList<Paragraph> Paragraphs { get; set; }

        public TableCell()
        {
            Tables = new XmlList<Table>();
            Tables.Changing += Tables_Changing;

            Paragraphs = new XmlList<Paragraph>();
            Paragraphs.Changing += Paragraphs_Changing;
        }

        public Table AddTable(int columns, int rows)
        {
            Table t = new Table(columns, rows);
            t.DocumentId = this.DocumentId;
            Tables.Add(t);
            return t;
        }

        public Paragraph AddParagraph()
        {
            Paragraph p = new Paragraph();
            p.DocumentId = this.DocumentId;
            Paragraphs.Add(p);
            return p;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddProperty(ITableCellPropElement element)
        {
            if (CellProperty == null)
            {
                CellProperty = new TableCellProperties();
            }

            var obj = CellProperty.Properties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as ITableCellPropElement).Copy(element);
            }
            else
            {
                CellProperty.Properties.Add(element);
                return true;
            }

            return false;
        }

        #region Events

        void Tables_Changing(object sender, ListEventArgs<Table> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                obj.DocumentId = this.DocumentId;
            }
        }

        void Paragraphs_Changing(object sender, ListEventArgs<Paragraph> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                obj.DocumentId = this.DocumentId;
            }
        }

        #endregion
    }

    [XmlRoot("tcPr", Namespace = Constants.WordMain)]
    public class TableCellProperties : XmlSerializable
    {
        [Optional]
        [XmlElement("", Namespace = Constants.WordMain)]
        public XmlList<ITableCellPropElement> Properties { get; set; }

        public TableCellProperties()
        {
            Properties = new XmlList<ITableCellPropElement>();
            Properties.Changing += Properties_Changing;
        }

        #region Events

        void Properties_Changing(object sender, ListEventArgs<ITableCellPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(ITableCellPropElement)))
                {
                    var element = Properties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + Properties.ToString());
                    }
                }
            }
        }

        #endregion
    }
}
